package com.kronos.game;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

/** 
 * @author Eric Ofman
 *
 */
public class ParallaxBackground {

	private ParallaxLayer[] layers;

	private OrthographicCamera camera;

	public ParallaxBackground(ParallaxLayer[] pLayers, OrthographicCamera cam) {
		this.layers = pLayers;
		this.camera = cam;
	} 
	
	public void render(SpriteBatch sb) {
		for (ParallaxLayer layer : layers) {
			if (layer.positionX > 1.0f) {
				layer.positionX = 0.0f;
			}
			int numDrawX = KronosGame.V_WIDTH / layer.texture.getWidth() + 1;

			for (int i = 0; i < numDrawX; i++) {
				sb.draw(layer.texture, i * layer.texture.getWidth() + (camera.position.x - camera.viewportWidth * camera.zoom / 2),
						layer.startY + (camera.position.y - camera.viewportHeight * camera.zoom / 2),
						layer.texture.getWidth(),
						layer.texture.getHeight(), 
						layer.positionX, 
						1,
						layer.positionX + 1, 
						0);
			}
		}
	}

	public void scrollRightX(float delta) {
		for (ParallaxLayer layer : layers) {
			layer.scrollRightX(delta);
		}
	}

	public void scrollRightY(float delta) {
		for (ParallaxLayer layer : layers) {
			layer.scrollRightY(delta);
		}
	}
	
	public void scrollLeftX(float delta) {
		for (ParallaxLayer layer : layers) {
			layer.scrollLeftX(delta);
		}
	}

	public void scrollLeftY(float delta) {
		for (ParallaxLayer layer : layers) {
			layer.scrollLeftY(delta);
		}
	}
}